package com.yaoli.niuke;

import java.util.ArrayList;
import java.util.Stack;

/**
 * Created by will on 2017/6/7.
 */
public class _026_Test {
    public static void main(String[] args) {
        int a [] = {1,2,3,4,5};
        int b [] = {4,5,3,2,1};

        System.out.println(IsPopOrder(a,b));
    }

    public static boolean IsPopOrder(int [] pushA,int [] popA) {
        //采用的思想：
        //使用栈，
        //每遍历一次，就push一个，
        //如果栈顶的元素跟 popA的的元素一样，那么就弹出来，index++

        //否则就继续
        Stack<Integer> stack = new Stack<Integer>();

        int index = 0;
        for(int i = 0 ; i < pushA.length ; i++){
            stack.push(pushA[i]);

            while(stack.size() != 0 && stack.peek() == popA[index]){
                stack.pop();
                index++;
            }
        }

        if(stack.size() == 0){
            return true;
        }else{
            return false;
        }
    }
}
